package cn.tradewin.advisor.pages.project.plan;

import java.util.List;

import org.apache.tapestry5.ValueEncoder;
import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;
import org.slf4j.Logger;

import cn.tradewin.advisor.persist.model.Project;
import cn.tradewin.advisor.persist.model.ProjectTaskKey;
import cn.tradewin.advisor.persist.model.Task;
import cn.tradewin.advisor.persist.services.MTaskService;
import cn.tradewin.advisor.persist.services.ProjectService;

public class TaskCategory {
	
	@SessionState
	private Project target;
	
	@Property
	private final TaskEncoder taskEncoder = new TaskEncoder();
	
	@Property
	private List<Task> availabledTasks;
	
	@Property
	private List<Task> assignedTasks;

	@Property
	private Task availabledTask;
	
	@Property
	private Task assignedTask;

	private List<Byte> selectedAvailabledTasks;
	
	private List<Byte> selectedAssignedTasks;

	@Inject
	private MTaskService taskService;
	
	@Inject
	private ProjectService projectService;

	@Environmental
	private JavaScriptSupport jss;
	
	@Inject
	private Logger log;
	
	private boolean inFormSubmission;

	public void onActivate() {
		inFormSubmission = false;
	}

	void onPrepareForSubmit() {
		inFormSubmission = true;
		selectedAvailabledTasks = CollectionFactory.newList();
		selectedAssignedTasks = CollectionFactory.newList();
	}
	
	void afterRender() {
		jss.require("submitproxy").with("add","padd");
		jss.require("submitproxy").with("remove","premove");
	}

	void onPrepareForRender() {
		availabledTasks = taskService.getTasksNotInProject(target.getId());
		assignedTasks = taskService.getTasksInProject(target.getId());
	}

	public boolean isAvlSelected() {
		return false;
	}

	public void setAvlSelected(boolean selected) {
		if (inFormSubmission) {
			if (selected) {
				selectedAvailabledTasks.add(availabledTask.getId());
			}
		}
	}
	
	public boolean isAsnSelected() {
		return false;
	}

	public void setAsnSelected(boolean selected) {
		if (inFormSubmission) {
			if (selected) {
				selectedAssignedTasks.add(assignedTask.getId());
			}
		}
	}

	
	void onSubmitFromAdd() {
		List<ProjectTaskKey> tasks = CollectionFactory.newList();
		ProjectTaskKey task = null;
		for (Byte taskId : selectedAvailabledTasks) {
			 task = new ProjectTaskKey();
			 task.setProjectId(new Integer(1));
			 task.setTaskId(taskId);
			 tasks.add(task);
		}
		projectService.addProjectTasks(tasks);
	}
	
	void onSubmitFromRemove() {
		List<ProjectTaskKey> tasks = CollectionFactory.newList();
		ProjectTaskKey task = null;
		for (Byte taskId : selectedAssignedTasks) {
			 task = new ProjectTaskKey();
			 task.setProjectId(target.getId());
			 task.setTaskId(taskId);
			 tasks.add(task);
		}
		projectService.removeProjectTasks(tasks);
	}
	
	private class TaskEncoder implements ValueEncoder<Task> {
		public String toClient(Task value) {
			return value.getId().toString();
		}
		public Task toValue(String clientValue) {
			Task m = new Task();
			m.setId(new Byte(clientValue));
			return m;
		}
	}
}